﻿using PLCS.App.Shared.Models;
using PLCS.Client.IService.ConfigManager;

using System.Reflection;

namespace PLCS.App.Shared.Service;

public class CacheSettingService
{
    private readonly IConfigService _configService;
    public CacheSetting cacheSetting = new CacheSetting();

    public CacheSettingService(IConfigService configService)
    {
        _configService = configService;
    }

    public async Task<CacheSetting> InitalCacheSetting()
    {
        var dblist = await _configService.GetListAsync();
        // 创建一个 cacheSetting 对象，设置其默认属性值为以下值
        var cacheSetting = new CacheSetting("Sienline", "Sienline", "Sienline", 17, 0, 1, 1, 1);
        // 获取 cacheSetting 类型的所有属性名称
        var properties = GetPropertyNames(cacheSetting.GetType());
        // 遍历属性名称列表
        foreach (var property in properties)
        {
            // 获取 cacheSetting 对象的指定属性
            PropertyInfo nameProperty = cacheSetting.GetType().GetProperty(property);
            // 将属性值转换成字符串类型
            string value = Convert.ToString(nameProperty.GetValue(cacheSetting));
            // 如果数据库中不存在名称为当前属性名称的 Config 对象，则插入一个新的对象
            if (!dblist.Any(x => x.Name == property))
                await _configService.CreateAsync(new Application.Contracts.ConfigApp.Dtos.ConfigEditDto()
                {
                    Name = property,
                    Value = value
                });
            // 如果数据库中存在名称为当前属性名称的 Config 对象，则将该对象的 Value 属性值赋值给 value 变量
            else
                value = dblist.FirstOrDefault(x => x.Name == property).Value;
            // 使用 SetValue 方法将属性值设置到 cacheSetting 对象上
            SetValue(cacheSetting, property, value);
        }
        // 返回更新后的 PLCInfo 对象
        this.cacheSetting = cacheSetting;
        return cacheSetting;
    }

    public async Task UpdateCacheSetting(CacheSetting model)
    {
        var dblist = await _configService.GetListAsync();
        var properties = GetPropertyNames(model.GetType());
        // 遍历属性名称列表
        foreach (var property in properties)
        {
            var lastModel = dblist.Single(x => x.Name == property);
            var lastData = lastModel.Value;
            PropertyInfo nameProperty = model.GetType().GetProperty(property);
            // 将属性值转换成字符串类型
            var value = Convert.ToString(nameProperty.GetValue(model));
            if (lastData != value)
                await _configService.UpdateAsync(new Application.Contracts.ConfigApp.Dtos.ConfigEditDto()
                {
                    Id = lastModel.Id,
                    Value = value,
                    Name = property,
                    Remark = lastModel.Remark,
                });
        }
    }

    private void SetValue(object obj, string propertyName, object propertyValue)
    {
        PropertyInfo property = obj.GetType().GetProperty(propertyName);
        property.SetValue(obj, Convert.ChangeType(propertyValue, property.PropertyType), null);
    }

    public List<string> GetPropertyNames(Type type)
    {
        List<string> propertyNames = new List<string>();
        foreach (var property in type.GetProperties())
        {
            propertyNames.Add(property.Name);
        }
        return propertyNames;
    }
}